<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Change_PW extends Page
{
       public $JSCompare = null;
       public $hfVerLength = null;
       public $hfNewLength = null;
       public $lblError2 = null;
       public $Image1 = null;
       public $lblPasswordError = null;
       public $edOldPw = null;
       public $lblError = null;
       public $edNewPw = null;
       public $edVerifyPw = null;
       public $Label10 = null;
       public $Label9 = null;
       public $Label8 = null;
       public $Button2 = null;
       public $Button1 = null;
       function edOldPwJSBlur($sender, $params)
       {

       ?>
       comparePass();

       <?php

       }


       function edVerifyPwJSKeyUp($sender, $params)
       {

       ?>
       var newPass2 = document.getElementById('edVerifyPw').value;
       document.getElementById('hfVerLength').value = newPass2.length;

       regex_pattern = "^[a-zA-Z0-9]*$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edVerifyPw').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblError2').innerHTML = "&darr;Alphanumeric characters only&darr;";
       }
       else
       {
        document.getElementById('lblError2').innerHTML = "";
       }
       <?php

       }

       function edNewPwJSKeyUp($sender, $params)
       {

       ?>
       var newPass1 = document.getElementById('edNewPw').value;
       document.getElementById('hfNewLength').value = newPass1.length;

       regex_pattern = "^[a-zA-Z0-9]*$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edNewPw').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblError').innerHTML = "&darr;Alphanumeric characters only&darr;";
       }
       else
       {
        document.getElementById('lblError').innerHTML = "";
       }
       <?php

       }

       function edVerifyPwJSBlur($sender, $params)
       {

       ?>
       var newPass1 = document.getElementById('edNewPw').value;
       var newPass2 = document.getElementById('edVerifyPw').value;

       if(newPass1!=newPass2)
       {
        document.getElementById('lblError2').innerHTML = "New Passwords did not match";
       }
       else
       {
        document.getElementById('lblError2').innerHTML = "";
       }

       //var newPass2 = document.getElementById('edVerifyPw').value;
       //if(newPass2.length>8 || 6>newPass2.length)
       //{
       //  document.getElementById('lblError2').innerHTML = "&darr;Password should be 6-8 characters&darr;";
       //}
       //if(newPass2.length>=6 && 8>=newPass2.length)
       //{
       //  document.getElementById('lblError2').innerHTML = "";
       //}
       <?php

       }

       function edNewPwJSBlur($sender, $params)
       {

       ?>
       var newPass1 = document.getElementById('edNewPw').value;
       if(newPass1.length>8 || 6>newPass1.length)
       {
         document.getElementById('lblError').innerHTML = "&darr;Password should be 6-8 characters&darr;";
       }
       if(newPass1.length>=6 && 8>=newPass1.length)
       {
         document.getElementById('lblError').innerHTML = "";
       }
       <?php

       }

       function Change_PWJSLoad($sender, $params)
       {
       //$this->clr_pw();
       ?>
       <?php

       }

       function Button2Click($sender, $params)
       {
       echo '<script language=javascript>
       window.close()</script>';


       }

       function clr_pw()
       {
       $this->edOldPw->Text='';
       $this->edNewPw->Text='';
       $this->edVerifyPw->Text='';
       $this->lblError->Caption='';
       $this->lblError2->Caption='';
       $this->lblPasswordError->Caption='';
       }



       function Change_PWCreate($sender, $params)
       {
       $this->edOldPw->Text='';
       $this->edNewPw->Text='';
       $this->edVerifyPw->Text='';
       $this->lblError->Caption='';
       $this->lblError2->Caption='';
       $this->lblPasswordError->Caption='';


       }



       function Button1Click($sender, $params)
       {
       $query_account = "select * from accounts_tbl where username='".$_SESSION['username']."' and company_id='".$_SESSION['company_id']."'";
       //die($query_account);
       $result_account = mysql_query($query_account) or die('SELECT ACCOUNTS'.mysql_error());
       $row_account = mysql_fetch_array($result_account);

       $pw = $row_account['password'];
       $password = sha1($this->edOldPw->getText());
       $newpw = $this->edNewPw->getText();
       $verifypw = $this->edVerifyPw->getText();

       $error = 0;
       $new_length = $this->hfNewLength->getValue();
       $ver_length = $this->hfVerLength->getValue();

       if($pw!=$password)
       {
        $this->lblPasswordError->Caption='&darr;Incorrect Old Password&darr;';
        $error = 1;
       }
       if($newpw!=$verifypw)
       {
        $this->lblError2->Caption='New Passwords did not match';
        $error = 1;
       }
       if($new_length<6 || $new_length>8)
       {
        $this->lblError->Caption='&darr;Password should be 6-8 characters&darr;';
        $error = 1;
       }
       if($ver_length<6 || $ver_length>8)
       {
        $this->lblError2->Caption='&darr;Password should be 6-8 characters&darr;';
        $error = 1;
       }

       if($error!=1)
       {
       $query_update_account = "update accounts_tbl set
       password='".sha1($this->edNewPw->Text)."'
       where username='".$_SESSION['username']."' and company_id='".$_SESSION['company_id']."'";
       mysql_query($query_update_account) or die(mysql_error());

       echo '<script language=javascript>
       alert("Changed Password Succesfully!")
       window.close()</script>';
       }
       }

}

global $application;

global $Change_PW;

//Creates the form
$Change_PW=new Change_PW($application);

//Read from resource file
$Change_PW->loadResource(__FILE__);

//Shows the form
$Change_PW->show();

?>